var mysql = require('mysql');
var superagent = require('superagent');

var msyqlConfig = require('./config').config.mysqlOptions;

var topicId = '19776749';

let offset = 0;
var questionList = [];

// var time0 = (new Date).getTime();
var time0;
fn1();

function fn1() {
    time0 = (new Date).getTime();

    questionList.length = 0;

    let connection1 = mysql.createConnection(msyqlConfig);
    let query1 = 'select count(*) as count from question_copy';
    connection1.connect();
    connection1.query(query1, function (err, res) {
        if (err) {
            // 
        } else {
            let count = res[0].count;
            offset = count;
            console.log('当前已有: ' + count);
            console.log();

            fn2();
        }
    });
    connection1.end();
}


function fn2() {
    let url = `https://www.zhihu.com/api/v4/topics/19776749/feeds/timeline_question?limit=10&offset=${offset}`;
    superagent.get(url).end(function (err, res) {
        if (err) {
            console.log(err);

        } else {
            let o = JSON.parse(res.text);
            // console.log(o);
            var paging = o.paging;
            var data = o.data;
            var list = data;
            list.forEach(v => {
                var target = v.target;
                var type = target.type;
                if (type === 'question') {
                    var id = target.id;
                    var title = target.title;
                    var follower_count = target.follower_count;
                    var answer_count = target.answer_count;
                    var created = target.created;
                    var author = target.author;
                    var authorId = author.id;
                    var authorName = author.name;

                    let item = [id, title, follower_count, answer_count, created, authorId, authorName];
                    questionList.push(item);
                }
            });
            // console.log(url);
            console.log('question lenght: ' + questionList.length);
            // console.log();

            if (questionList.length < 200) {
                offset += 10;
                fn2();
            } else {
                console.log();
                fn3();
            }
        }
    })
}

function fn3() {
    // console.log(questionList);

    var connection2 = mysql.createConnection(msyqlConfig);
    connection2.connect();
    var query2 = 'insert ignore into question_copy(id,title,follower_count,answer_count,created,author_id,author_name) values ?';
    connection2.query(query2, [questionList], function (err, res) {
        if (err) {
            console.log(err);

        } else {
            console.log(res);
            console.log();
            var time1 = (new Date).getTime();
            console.log('本次用时: ' + ((time1 - time0) / 1000) + '秒');
            console.log();

            fn1();
        }
    });
    connection2.end();
}